<template>
  <transition name="fade">
    <aside v-if="visible" class="setting-box">
      <header>
        <h4>高亮模式</h4>
        <i @click="handleClose" class="icon el-icon-close"></i>
      </header>
      <section class="switch-box">
        <el-switch
          v-model="open"
          size="medium"
          active-color="#13ce66"
          inactive-color="#cddce2"
          class="elswitch">
        </el-switch>
        <span class="switch-txt" @click="open=!open">{{switchTxt}}</span>
      </section>
      <ul class="mode-list">
        <li v-for="(item, index) in modeList" :key="index">
          <el-color-picker
            v-model="item.color"
            size="mini"
            show-alpha
            :predefine="predefineColors"
            class="color-picker">
          </el-color-picker>
          <el-input v-model="item.words" size="small" placeholder="" class="elinput"></el-input>
        </li>
      </ul>
    </aside>
  </transition>
</template>

<script>
import {mapState} from 'vuex'
export default {
  data() {
    return {
      visible: false,
      open: false,
      modeList: [
        {color: '#ff4500', words: ''},
        {color: '#ff8c00', words: ''},
        {color: '#c71585', words: ''},
        {color: '#2aa42a', words: ''},
        {color: '#1e90ff', words: ''},
      ],
      predefineColors: [
        '#ff4500',
        '#ff8c00',
        '#ffd700',
        '#90ee90',
        '#00ced1',
        '#1e90ff',
        '#c71585',
        '#2aa42a',
      ],
      switchTxt: ''
    }
  },
  computed: {
    ...mapState(['highlightModeSettings'])
  },
  methods: {
    init() {
      this.visible = !this.visible
    },
    getHighlightMode() {
      let highlightMode = this.highlightModeSettings
      if (Object.keys(highlightMode).length <= 0 && !highlightMode['modes']) {
        this.$store.commit('setHighlightModeSettings', {
          modes: this.modeList,
          open: this.open
        })
      } else {
        this.modeList = this.highlightModeSettings.modes
        this.open = this.highlightModeSettings.open
      }
      this.switchTxt = this.open ? '开启' : '关闭'
    },
    handleClose() {
      this.visible = false
    },
    hideBox() {
      let ths = this
      let callback = function (e) {
        ths.visible = false
      }
      document.getElementById('app').removeEventListener('click', callback, false)
      document.getElementById('app').addEventListener('click', callback, false)
    }
  },
  watch: {
    modeList: {
      deep: true,
      handler: function(val) {
        this.$store.commit('setHighlightModeSettings', {
          modes: this.modeList,
          open: this.open
        })
        this.switchTxt = this.open ? '开启' : '关闭'
      }
    },
    open() {
      this.$store.commit('setHighlightModeSettings', {
        modes: this.modeList,
        open: this.open
      })
      this.switchTxt = this.open ? '开启' : '关闭'
    }
  },
  beforeMount() {
    this.getHighlightMode()
  },
  mounted() {
    // this.globalClick(this.hideBox)
  }
}
</script>

<style lang="scss" scoped>
.setting-box {
  position: fixed;
  top: 37.4vh;
  right: 0;
  z-index: 3;
  width: 20%;
  min-width: 200px;
  max-width: 300px;
  // height: 60vh;
  max-height: 400px;
  border-top-left-radius: 8px;
  border-bottom-left-radius: 8px;
  // margin-top: 25vh;
  // margin-top: calc((50vh - 50%));
  box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
  background-color: $color2;
  overflow-y: auto;
  header {
    position: relative;
    height: 40px;
    padding: 0 20px;
    font-size: 16px;
    color: $color6;
    h4 {
      line-height: 40px;
      padding-right: 40px;
      margin-top: 0;
      margin-bottom: 0;
    }
    .icon {
      position: absolute;
      top: 0;
      right: 0;
      width: 40px;
      height: 40px;
      line-height: 40px;
      text-align: center;
      font-size: 20px;
      cursor: pointer;
      &:hover {
        color: $mainColor;
      }
    }
  }
  .switch-box {
    position: relative;
    padding: 4px 20px;
    margin-bottom: 10px;
    text-align: right;
    .elswitch {
      ::v-deep {
        .el-switch__core {
          width: 64px !important;
          height: 24px;
          line-height: 24px;
          border-radius: 20px;
          &:after {
            // width: 16px;
            // height: 18px;
            top: 3px;
          }
        }
      }
    }
    .switch-txt {
      position: absolute;
      top: 2px;
      right: 20px;
      width: 64px;
      height: 24px;
      line-height: 24px;
      text-align: center;
      font-size: 11px;
      color: $color2;
      cursor: pointer;
    }
  }
  .mode-list {
    position: relative;
    padding: 0 20px 20px;
    li {
      position: relative;
      display: flex;
      align-items: center;
      justify-content: center;
      height: 40px;
      // border: 1px solid $color152;
      // margin-bottom: 14px;
      .color-picker {
        position: absolute;
        left: 5px;
        z-index: 2;
        height: 25px !important;
        ::v-deep {
          .el-color-picker__trigger {
            width: 24px !important;
            height: 24px !important;
            border: none !important;
          }
          .el-color-picker__color {
            border-color: $color141;
          }
          .el-color-picker__icon, .el-color-picker__empty {
            display: none !important;
          }
          .el-color-picker__color {
            border: none !important;
          }
          .el-color-picker__color-inner {
            border-radius: 4px;
          }
        }
      }
      ::v-deep {
        .el-input__inner {
          height: 32px !important;
          padding-left: 40px !important;
        }
      }
    }
  }
}
// .fade-enter {
//   right: -400px;
// }
.fade-enter-active, .fade-leave-active {
  transition: all .3s;
}
.fade-enter, .fade-leave-to {
  right: -200px;
  opacity: 0;
}
</style>